<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>blog_list</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/bolg_list.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
    <script src="common.js"></script>
</head>
<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="头像.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="logout">注销</a>
    </div>

    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left">
            <div class="card">
                <img src="头像.jpg" class="avtar" alt="">
                <h3></h3>
                <a href="https://gitee.com/fang-qiuhui">gitee 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span>2</span>
                    <span>1</span>
                </div>
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div class="container-right">
            <!-- 每篇博客包含标题，摘要，时间 -->
            <!-- <div class="blog">
                <div class="title">我的第一篇博客</div>
                <div class="date">2022-06-23</div>
                <div class="desc">
                    关注Stella_sss，一起学编程！Lorem ipsum dolor sit amet consectetur adipisicing elit. Deserunt aperiam voluptatum dolore suscipit quasi temporibus hic nemo aut voluptate eos impedit, quis repellendus doloribus sapiente similique veritatis atque aspernatur esse.
                </div>
                <a href="blog_content.html?blogId=1" class="detail">查看全文 &gt;&gt;</a>
            </div>
            <div class="blog">
                <div class="title">我的第二篇博客</div>
                <div class="date">2022-07-23</div>
                <div class="desc">
                    关注Stella_sss，一起学编程！Lorem ipsum dolor sit amet consectetur adipisicing elit. Deserunt aperiam voluptatum dolore suscipit quasi temporibus hic nemo aut voluptate eos impedit, quis repellendus doloribus sapiente similique veritatis atque aspernatur esse.
                </div>
                <a href="blog_content.html?blogId=2" class="detail">查看全文 &gt;&gt;</a>
            </div> -->
        </div>
    </div>

    

    <script>
        function getBlogList() {
            $.ajax({
                type: 'get',
                url: 'blog',
                success: function(body) {
                    // 获取到的body是一个js对象数组，每个元素就是一个js对象
                    // 根据这个对象构造div
                    // 1. 先把.container-right 里原有的内容清空
                    let rightDiv = document.querySelector('.container-right');
                    rightDiv.innerHTML = '';
                    // 2.遍历body，构造出一个个的blogDiv
                    for (let blog of body) {
                        let blogDiv = document.createElement('div');
                        blogDiv.className = 'blog';
                        // 构造标题
                        let titleDiv = document.createElement('div');
                        titleDiv.className = 'title';
                        titleDiv.innerHTML = blog.title;
                        blogDiv.appendChild(titleDiv);
                        // 构造发布时间
                        let dateDiv = document.createElement('div');
                        dateDiv.className = 'date';
                        dateDiv.innerHTML = blog.postTime;
                        blogDiv.appendChild(dateDiv);
                        // 构造博客的摘要
                        let contentDiv = document.createElement('div');
                        contentDiv.className = 'desc';
                        contentDiv.innerHTML = blog.content;
                        blogDiv.appendChild(contentDiv);
                        // 构造查看全文
                        let a = document.createElement('a');
                        a.className = 'detail';
                        a.innerHTML = '查看全文 &gt;&gt;';
                        a.href = 'blog_content.html?blogId=' + blog.blogId;
                        blogDiv.appendChild(a);
                        // 把blogDiv挂到dom树上
                        rightDiv.appendChild(blogDiv);
                    }
                },
                error: function() {
                    alert("获取博客列表失败！");
                }
            });
        }
        getBlogList();
        getUserInfo('blog_list.html');
    </script>
    
</body>
</html>